package com.mi.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.mi.dao.PhoneDao;
import com.mi.entity.Phone;

/**
 * 跟手机有关的dao
 * @author muchengxiang
 *
 */
@Repository
public class PhoneDaoImpl implements PhoneDao{

	@Resource  
	private HibernateTemplate template;
	/**
	 * 返回销量前五的手机
	 */
	@Override
	public List<Phone> getSalesTop5() {
		Session session = template.getSessionFactory().openSession();
		Query<Phone> query = session.createQuery("select new Phone(name,zuidijia) from Phone order by pinglunshu desc");
		query.setMaxResults(5);
		query.setFirstResult(0);
		List<Phone> list = query.list();
		session.close();
		return list;
	}
	
	/**
	 * 返回评论最多的10个手机
	 */
	@Override
	public List<Phone> getCommentNumTop10() {
		Session session = template.getSessionFactory().openSession();
		Query<Phone> query = session.createQuery("select new Phone(name,zuidijia,pinglunshu) from Phone order by xiaoliang desc");
		query.setMaxResults(10);
		query.setFirstResult(0);
		List<Phone> list = query.list();
		session.close();
		return list;
	}

}
